﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTO;

namespace DAO
{
    public class DanhMucConDAO
    {
        public static List<DanhMucConDTO> layDSDanhMucCon(int maDanhMucChinh)
        {
            List<DanhMucConDTO> result = new List<DanhMucConDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = (from dsDanhMuc in dbRaoVat.DANHMUCCONs.Where(dm => dm.Deleted.Equals(0) &&
                             dm.MaDanhMucChinh == maDanhMucChinh).OrderBy(dm=>dm.ThuTuTrongDanhMucChinh)
                         select new
                         {
                             dsDanhMuc.TenDanhMucCon,
                             dsDanhMuc.MaDanhMucCon,
                             dsDanhMuc.MaDanhMucChinh,
                             dsDanhMuc.ThuTuTrongDanhMucChinh
                         }).ToList();
            foreach (var ds in query)
            {
                result.Add(new DanhMucConDTO(ds.MaDanhMucCon, ds.TenDanhMucCon, (int)ds.MaDanhMucChinh, (int)ds.ThuTuTrongDanhMucChinh, 0));
            }

            return result;
        }

        public static List<DanhMucConDTO> getAll()
        {
            List<DanhMucConDTO> result = new List<DanhMucConDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCONs.Where(dm => dm.Deleted.Equals(0)).ToList();

            foreach (var ds in query)
            {
                DanhMucConDTO dto = new DanhMucConDTO();

                dto.MaDanhMucCon = ds.MaDanhMucCon;                
                dto.TenDanhMucCon = ds.TenDanhMucCon;
                dto.MaDanhMucChinh = ds.MaDanhMucChinh.Value;

                result.Add(dto);
            }

            return result;
        }

        public static DanhMucConDTO getAlongCode(int maDanhMucCon)
        {
            DanhMucConDTO result = new DanhMucConDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCONs.Where(dm => dm.Deleted.Equals(0) &&
                dm.MaDanhMucCon == maDanhMucCon).Single();

            result.MaDanhMucCon = query.MaDanhMucCon;
            result.TenDanhMucCon = query.TenDanhMucCon;
            result.MaDanhMucChinh = query.MaDanhMucChinh.Value;

            return result;
        }

        public static void Insert(DanhMucConDTO _insert)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DANHMUCCON danhmuccon = new DANHMUCCON();
            danhmuccon.TenDanhMucCon = _insert.TenDanhMucCon;
            danhmuccon.MaDanhMucChinh = _insert.MaDanhMucChinh;
            danhmuccon.ThuTuTrongDanhMucChinh = _insert.ThuTuTrongDanhMucChinh;
            danhmuccon.Deleted = _insert.Deleted;

            dbRaoVat.DANHMUCCONs.InsertOnSubmit(danhmuccon);
            dbRaoVat.SubmitChanges();
        }

        public static void Delete(int maDanhMucCon)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DANHMUCCON _delete = (from n in dbRaoVat.DANHMUCCONs
                                 where n.MaDanhMucCon == maDanhMucCon && n.Deleted == 0
                                 select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(DanhMucConDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DANHMUCCON _update = (from n in dbRaoVat.DANHMUCCONs
                                  where n.MaDanhMucCon == dto.MaDanhMucCon
                                  select n).Single();

            _update.TenDanhMucCon = dto.TenDanhMucCon;
            _update.MaDanhMucChinh = dto.MaDanhMucChinh;
            _update.ThuTuTrongDanhMucChinh = dto.ThuTuTrongDanhMucChinh;
            _update.Deleted = dto.Deleted;
            
            dbRaoVat.SubmitChanges();
        }

        public static int getMa(string tenDanhMuc)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCONs.Where(dm => 
                dm.TenDanhMucCon == tenDanhMuc && dm.Deleted == 0).First();

            return query.MaDanhMucCon;
        }

        public static string getTenDanhMuc(int maDanhMuc)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCONs.Where(dm =>
                dm.MaDanhMucCon == maDanhMuc).First();

            return query.TenDanhMucCon;
        }

        public static int getMaDanhMucChinh(int maDanhMucCon)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.DANHMUCCONs.Where(dm =>
                dm.MaDanhMucCon == maDanhMucCon).First();

            return (int)query.MaDanhMucChinh;
        }
    }
}
